package top.gangster.lunan.userservice.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.gangster.lunan.result.Result;
import top.gangster.lunan.userservice.annotation.LoginToken;
import top.gangster.lunan.userservice.annotation.PermissionLimit;
import top.gangster.lunan.userservice.entity.Role;
import top.gangster.lunan.userservice.entity.User;
import top.gangster.lunan.userservice.entity.UserAdd;
import top.gangster.lunan.userservice.entity.UserSave;
import top.gangster.lunan.userservice.entity.vo.UserVo;
import top.gangster.lunan.userservice.service.TokenService;
import top.gangster.lunan.userservice.service.UserService;


@RestController
@RequestMapping("/userservice/user")
public class UserController {

  @Autowired
  private UserService userService;
  @Autowired
  private TokenService tokenService;

  /**
   * 登录
   * @param users 用户名密码
   * @return Result
   */
  @PostMapping("/login")
  public Result userLoginController(@RequestBody User users) {
    //拿到工号和密码
    String uid = users.getUid();
    String password = users.getPassword();
    //验证工号密码是否正确
    User user=userService.verifyUser(uid, password);
    if(user==null){
      return Result.error().message("登陆失败，请检查用户工号密码是否正确！");
    }
    //验证用户是否被冻结
    else if (!user.getIsEnable())
    {
      return Result.error().message("登陆失败，用户已被冻结（not enable）");
    }
    //将token以及用户id返回
    else {
      String token = tokenService.getToken(user);
      return Result.ok().data("token",token).data("userid",user.getUserId());
    }
  }

  /**
   * 查找指定id的用户
   * @param id 用户id
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_STANDARD)
  @GetMapping("/{id}")
  public Result getUserById(@PathVariable String id) {
    //通过id查找用户
    User user = userService.getById(id);
    if (user == null) {
      return Result.error().message("未查找到任何用户");
    }
    return Result.ok().data("user",user);
  }

  /**
   * 查看用户列表
   * @param current 当前页
   * @param size 当前页面大小
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_STANDARD)
  @GetMapping("/userlist")
  public Result getUserlistController(@RequestParam int current,@RequestParam int size){
    //分页查找用户列表
    UserVo userList = userService.getUserList(current,size);
    if (userList == null)
    {
      return Result.error().message("查询失败，未查找到任何用户");
    }
    return Result.ok().data("userList",userList);

  }

  /**
   * 通过用户名查看用户列表
   * @param current 当前页
   * @param size 当前页面大小
   * @param username 用户名
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_STANDARD)
  @GetMapping("/userByname")
  public Result getUserByNameController(@RequestParam int current,@RequestParam int size,@RequestParam String username){
    //分页查找通过用户名
    UserVo userSbyname = userService.getUserByName(current,size,username);
    if (userSbyname == null)
    {
      Result.error().message("查询失败，未查找到任何用户");
    }
    return Result.ok().data("userList",userSbyname);
  }

  /**
   * 通过phonenumber查看用户列表
   * @param current 当前页
   * @param size 当前页面大小
   * @param phonenumber 电话号码
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_STANDARD)
  @GetMapping("/userByphone")
  public Result getUserByPhoneController(int current,int size,String phonenumber){
    //分页查找通过手机号
    UserVo userByphone =userService.getUserByPhone(current,size,phonenumber);
    if (userByphone == null){
      return Result.error().message("查询失败，未查找到任何用户");
    }
    return Result.ok().data("userList",userByphone);
  }

  /**
   * 通过deptname查看用户列表
   * @param current 当前页
   * @param size 当前页面大小
   * @param deptname 部门名字
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_STANDARD)
  @GetMapping("/userBydept")
  public Result getUserByDeptController(int current,int size,String deptname){
    //分页查找通过部门名
    UserVo userBydept = userService.getUserByDept(current,size,deptname);
    if (userBydept == null){
      return Result.error().message("查询失败，未查找到任何用户");
    }
    return Result.ok().data("userList",userBydept);
  }

  /**
   * 通过deptid查看用户列表，不分页
   * @param deptId 部门id
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_STANDARD)
  @GetMapping("/userByDept/{deptId}")
  public Result getUserByDeptController(@PathVariable String deptId){
    //分页查找通过部门名
    List<UserSave> userBydept = userService.getUserByDept(deptId);
    if (userBydept == null){
      return Result.error().message("查询失败，未查找到任何用户");
    }
    return Result.ok().data("userList",userBydept);
  }

  /**
   * 通过角色查看用户列表分页版本
   * @param current 当前页
   * @param size 当前页面大小
   * @param role 角色
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_STANDARD)
  @GetMapping("/userByrole")
  public Result getUserByRoleController(int current,int size,String role){
    //分页查找通过角色名
    UserVo userByrole = userService.getUserByRole(current,size,role);
    if (userByrole == null){
      return Result.error().message("查询失败，未查找到任何用户");
    }
    return Result.ok().data("userList",userByrole);
  }

  /**
   * 通过角色id查看用户列表不分页版本
   * @param roleId 角色id
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_STANDARD)
  @GetMapping("/userByRole/{roleId}")
  public Result getUserByRoleController(@PathVariable String roleId){
    //分页查找通过角色名
    List<UserSave> userByrole = userService.getUserByRole(roleId);
    if (userByrole == null){
      return Result.error().message("查询失败，未查找到任何用户");
    }
    return Result.ok().data("userList",userByrole);
  }

  /**
   * 添加用户
   * @param users 用户
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_ADMIN)
  @PostMapping("/adduser")
  public Result addUserController(@RequestBody UserAdd users){
    //添加用户
    boolean b = userService.addUser(users);
    if (!b){
      return Result.error().message("新增用户失败！请检查部门，角色等信息输入是否正确！");
    }


    return Result.ok().message("新增用户成功！");
  }

  /**
   * 修改用户
   * @param users 用户
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_ADMIN)
  @PutMapping("/modify")
  public Result modifyUserController(@RequestBody UserAdd users){
    //修改用户
    boolean b = userService.modifyUser(users);
    if (!b){
      return Result.error().message("修改用户失败！！");
    }
    return Result.ok().message("修改用户成功！");
  }

  /**
   * 删除用户
   * @param users 用户
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_ADMIN)
  @DeleteMapping("/delete")
  public Result deleteUserController(@RequestBody UserAdd users){
    //删除用户
    boolean b = userService.deleteUser(users);
    if (!b){
      return Result.error().message("删除用户失败！！");
    }
    return Result.ok().message("删除用户成功！");
  }

  /**
   * 冻结用户
   * @param users 用户
   * @return Result
   */
  @LoginToken
  @PermissionLimit(role = PermissionLimit.ROLE_ADMIN)
  @PatchMapping("/freeze")
  public Result freezeUserController(@RequestBody User users){
    //冻结用户
    boolean b = userService.freezeUser(users);
    if (!b){
      return Result.error().message("冻结/解冻用户失败！！");
    }
    return Result.ok().message("冻结/解冻用户成功！");
  }

  /**
   * 权限测试接口
   * @return Result
   */
  @PermissionLimit(role = PermissionLimit.ROLE_ADMIN)
  @LoginToken
  @PostMapping("/getMessage")
  public String getMessage(){
    return "你已通过验证";
  }

  /**
   * 权限测试接口
   * @return Result
   */
  @PermissionLimit(role = PermissionLimit.ROLE_STANDARD)
  @LoginToken
  @PostMapping("/getMessages")
  public String getMessages(){
    return "你已通过验证s";
  }



}




